package com.lulufind.lulu.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.lulufind.lulu.model.City;
import com.lulufind.lulu.model.Result;
import com.lulufind.lulu.model.School;
import com.lulufind.lulu.model.Temp;
import com.lulufind.lulu.model.User;
import com.lulufind.lulu.model.UserFollow;
import com.lulufind.lulu.model.UserPostFollow;
import com.lulufind.lulu.model.UserPostJoin;
import com.lulufind.lulu.service.TempService;
import com.lulufind.lulu.service.UserService;
import com.lulufind.lulu.vo.CoterieUserInfoVO;
import com.lulufind.lulu.vo.DemandUserInfoVO;
import com.lulufind.lulu.vo.TrendsVO;
import com.lulufind.lulu.vo.UserBindInfoVO;
import com.lulufind.lulu.vo.UserFollowInfoVO;
import com.lulufind.lulu.vo.UserInfoVO;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	UserService userService;
	@Autowired
	TempService tempService;
	
	@ResponseBody
	@RequestMapping(value="/sendCode",produces="application/json;charset=UTF-8")
	public String sendCode(Temp temp){
		
		System.out.println(temp.getTempPhone());
		
		Result<Temp> rs;
		
		rs = tempService.sendCode(temp);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/register",produces="application/json;charset=UTF-8")
	public String register(User user, String userCode){
		
		Result<UserInfoVO> rs = userService.register(user, userCode);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/registerTemp",produces="application/json;charset=UTF-8")
	public String registerTemp(User user, String temp){
		
		Result<String> rs = userService.registerTemp(user, temp);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/loginUseToken",produces="application/json;charset=UTF-8")
	public String loginUseToken(int userId, String userToken){
		
		Result<UserInfoVO> rs = userService.loginUseToken(userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/loginUsePhone",produces="application/json;charset=UTF-8")
	public String loginUsePhone(String userPhone, String userPasswd){
		
		Result<UserInfoVO> rs = userService.loginUsePhone(userPhone, userPasswd);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/loginUseThird",produces="application/json;charset=UTF-8")
	public String loginUseThird(User user){
		
		Result<UserInfoVO> rs = userService.loginThird(user);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/bindPhone",produces="application/json;charset=UTF-8")
	public String bindPhone(Integer userId, String userToken, String userPhone, String userCode){
		
		Result<User> rs = userService.bindPhone(userId, userToken, userPhone, userCode);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/changeUserInfo",produces="application/json;charset=UTF-8")
	public String changeUserInfo(User user){
		
		Result<User> rs = userService.changeUserInfo(user);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/userFollow",produces="application/json;charset=UTF-8")
	public String userFollow(UserFollow userFollow, String userToken, int followSwitch){
		
		Result<UserFollow> rs;
		
		if(followSwitch==1){
			rs = userService.addUserFollow(userFollow, userToken);
		} else {
			rs = userService.delUserFollow(userFollow, userToken);
		}
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/userPostFollow",produces="application/json;charset=UTF-8")
	public String userPostFollow(UserPostFollow userPostFollow, String userToken, int followSwitch){
		
		Result<UserPostFollow> rs;
		
		if(followSwitch==1){
			rs = userService.addUserPostFollow(userPostFollow, userToken);
		} else {
			rs = userService.delUserPostFollow(userPostFollow, userToken);
		}
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/userPostJoin",produces="application/json;charset=UTF-8")
	public String userPostJoin(UserPostJoin userPostJoin, String userToken, int joinSwitch, String postPasswd){
		
		Result<UserPostJoin> rs;
		
		if(joinSwitch==1){
			rs = userService.addUserPostJoin(userPostJoin, userToken, postPasswd);
		} else {
			rs = userService.delUserPostJoin(userPostJoin, userToken);
		}
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/userPostWeeding",produces="application/json;charset=UTF-8")
	public String userPostWeeding(Integer postId, Integer userId, String userToken){
		
		Result<String> rs = userService.weedingPost(postId, userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserInfo",produces="application/json;charset=UTF-8")
	public String getUserInfo(Integer userId, String userToken, Integer targetUserId){
		
		Result<UserInfoVO> rs = userService.getUserInfo(userId, userToken, targetUserId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserBindInfo",produces="application/json;charset=UTF-8")
	public String getUserBindInfo(Integer userId, String userToken){
		
		Result<UserBindInfoVO> rs = userService.getUserBindInfo(userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserDemandInfo",produces="application/json;charset=UTF-8")
	public String getUserDemandInfo(int userId, String userToken, int targetUserId){
		
		Result<DemandUserInfoVO> rs = userService.getUserDemandInfo(userId, userToken, targetUserId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/forgetPasswdSendCode",produces="application/json;charset=UTF-8")
	public String forgetPasswdSendCode(Temp temp){
		
		Result<Temp> rs = userService.forgetPasswdSendCode(temp);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/forgetPasswdCheck",produces="application/json;charset=UTF-8")
	public String forgetPasswdCheck(Temp temp){
		
		Result<Temp> rs = userService.forgetPasswdCheck(temp);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/forgetPasswdSet",produces="application/json;charset=UTF-8")
	public String forgetPasswdSet(Temp temp, String passwd){
		
		Result<Temp> rs = userService.forgetPasswdSet(temp, passwd);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/changePasswd",produces="application/json;charset=UTF-8")
	public String changePasswd(User user, String newPasswd){
		
		Result<User> rs = userService.changePasswd(user, newPasswd);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getFollowToUserInfoList",produces="application/json;charset=UTF-8")
	public String getFollowToUserInfoList(int userId, String userToken, int targetUserId, int start, int num){
		
		Result<List<UserFollowInfoVO>> rs = userService.getFollowToUserInfoList(userId, userToken, targetUserId, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getFollowFromUserInfoList",produces="application/json;charset=UTF-8")
	public String getFollowFromUserInfoList(int userId, String userToken, int targetUserId, int start, int num){
		
		Result<List<UserFollowInfoVO>> rs = userService.getFollowFromUserInfoList(userId, userToken, targetUserId, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getWalletNum",produces="application/json;charset=UTF-8")
	public String getWalletNum(int userId, String userToken){
		
		Result<Double> rs = userService.getWalletNum(userId, userToken);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getHotUser",produces="application/json;charset=UTF-8")
	public String getHotUser(Integer userId, Integer start, Integer num){
		
		Result<List<UserInfoVO>> rs = userService.getHotUserInfo(userId, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getFriends",produces="application/json;charset=UTF-8")
	public String getFriends(Integer userId, String userToken, Integer start, Integer num){
		
		Result<List<UserInfoVO>> rs = userService.getFriends(userId, userToken, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getUserTrends",produces="application/json;charset=UTF-8")
	public String getUserTrends(Integer userId, String userToken, Integer start, Integer num){
		
		Result<List<TrendsVO>> rs = userService.getUserTrends(userId, userToken, start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getHotCoterieUserInfo",produces="application/json;charset=UTF-8")
	public String getHotCoterieUserInfo(Integer start, Integer num, Integer userId){
		
		Result<List<CoterieUserInfoVO>> rs = userService.getHotCoterieUserInfo(start, num, userId);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getHotCity",produces="application/json;charset=UTF-8")
	public String getHotCity(Integer start, Integer num){
		
		Result<List<City>> rs = userService.getHotCity(start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}
	
	@ResponseBody
	@RequestMapping(value="/getHotSchool",produces="application/json;charset=UTF-8")
	public String getHotSchool(Integer start, Integer num){
		
		Result<List<School>> rs = userService.getHotSchool(start, num);
		
		Gson gson = new Gson();
		String s = gson.toJson(rs);
		return s;
	}

}
